import Ember from 'ember';
import BaseBusiness from '../base-business';
import Pagination from '../pagination';

export default BaseBusiness.extend(Pagination, {
    dataService: Ember.inject.service("date-service"),
    header_title: '违规记录',
    model() {
        return {};
    },
    buildQueryParams: function() {
        var params = this.pagiParamsSet();
        var curController = this.getCurrentController();
        let chooseTabFlag = curController.get('chooseTabFlag');
        var filter = {};
        var sort;
        console.log("record-violation1111", this.get("dataService"));
        if (curController) {
            if(curController.get('chooseTabFlag')){
              if(chooseTabFlag == "chooseTabOne"){
                filter = $.extend({}, filter, {'[type][typecode]':"violat1"});
              }else if(chooseTabFlag == "chooseTabTwo"){
                filter = $.extend({}, filter, {'[type][typecode]':"violat2"});
              }else{
                filter = $.extend({}, filter, {'[type][typecode]':"violat3"});
              }
            }
            if(curController.get('warningCustomer')){
              filter = $.extend({}, filter, {'[customer][id]':curController.get('warningCustomer.id')});
            }
            if(curController.get('staff')){
              filter = $.extend({}, filter, {'[employee][id]':curController.get('staff.id')});
            }
            //时间条件搜索
            var queryCondition = curController.get('queryCondition');
            if (queryCondition) {
                var compareDate = null;
                if (queryCondition === "today") {
                    compareDate = this.get("dataService").getTodayTimestamp();
                }
                if (queryCondition === "seven") {
                    compareDate = this.get("dataService").getDaysBeforeTimestamp(7);
                }
                if (queryCondition === "thirty") {
                    compareDate = this.get("dataService").getDaysBeforeTimestamp(30);
                }
                filter = $.extend({}, filter, {
                    'createTime@$gte': compareDate
                });
            }
            //自定义日期搜索
            var conBeginDate = curController.get("beginDate");
            var conEndDate = curController.get("endDate");
            if (conBeginDate && conEndDate) {
              var beginCompare = this.get("dataService").getFirstSecondStampOfDay(conBeginDate);
              var endCompare = this.get("dataService").getLastSecondStampOfDay(conEndDate);
              filter = $.extend({}, filter, {
                  'createTime@$gte': beginCompare
              }, {
                  'createTime@$lte': endCompare
              });
            }
        }
        params.filter = filter;
        sort = {
            '[createTime]': 'desc',
        };
        params.sort = sort;
        return params;
    },
    doQuery: function() {
        var params = this.buildQueryParams();
        let warningList;
        var curController = this.getCurrentController();
        let chooseTabFlag = curController.get('chooseTabFlag');
        warningList = this.findPaged('record-violation', params, function(warningList) {});
        console.log("warningList in three:",warningList);
        curController.set("warningList", warningList);
    },
    setupController: function(controller, model) {
        controller.set("chooseTabFlag","chooseTabOne");
        this.doQuery();
        this._super(controller, model);
        let filterCustomer;
        filterCustomer = $.extend({}, filterCustomer, {
            '[customerStatus][typecode@$like]@$or1---1': 'customerStatusIn'
        });
        filterCustomer = $.extend({}, filterCustomer, {
            '[customerStatus][typecode@$like]@$or1---2': 'customerStatusTry'
        });
        //查询所有的客户信息
        this.store.query('customer', {filterCustomer}).then(function(customerList) {
            customerList.forEach(function(customer) {
                customer.set('sortName', pinyinUtil.getFirstLetter(customer.get("name")));
            });
            controller.set('customerList', customerList);
        });
        //员工列表
        this.store.query('employee',{filter:{staffStatus:{'typecode@$not':Constants.staffStatusLeave}}}).then(function(staffList){
          staffList.forEach(function(staff){
            staff.set('namePinyin',pinyinUtil.getFirstLetter(staff.get("name")));
          });
          controller.set('staffList',staffList);
        });
    }
});
